﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using FX.CC.Andon.Entity.BasicDataManage;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.BasicDataManage
{
    public class ShiftService : DbHelperBase
    {
        public int GetShiftCount(ShiftQcEntity qcmodel)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  count(*) AS [count] FROM UT_Shift AS A WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.ShiftName))
            {
                strSql.Append(" AND ShiftName like '%" + qcmodel.ShiftName + "%'");
            }
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public IList<ShiftEntity> GetShiftList(ShiftQcEntity qcmodel)
        {
            var list = new List<ShiftEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            int start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            int end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by InputTime) AS idx,[ShiftID],[ShiftName],[RecStatus],[InputTime] FROM [UT_Shift] ");
            strSql.Append(" WHERE 1=1 ");
            if (!string.IsNullOrEmpty(qcmodel.ShiftName))
            {
                strSql.Append(" AND ShiftName like '%" + qcmodel.ShiftName + "%'");
            }
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ShiftEntity();
                            if (reader["ShiftID"] != DBNull.Value)
                                model.ShiftID = new Guid(reader["ShiftID"].ToString());
                            model.ShiftName = reader["ShiftName"] == obj ? null : Convert.ToString(reader["ShiftName"]);
                            if (reader["RecStatus"] != DBNull.Value)
                                model.RecStatus = Convert.ToBoolean(reader["RecStatus"].ToString());
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public IList<ShiftEntity> GetShiftListByCombobox()
        {
            var list = new List<ShiftEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT ShiftID,ShiftName FROM UT_Shift ");
            strSql.Append(" order by InputTime ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new ShiftEntity();
                            if (reader["ShiftID"] != DBNull.Value)
                                model.ShiftID = new Guid(reader["ShiftID"].ToString());
                            model.ShiftName = reader["ShiftName"] == obj ? null : Convert.ToString(reader["ShiftName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }

        public bool AddShift(ShiftEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [UT_Shift]([ShiftID],[ShiftName],[RecStatus]) ");
            strSql.Append(" VALUES(@ShiftID,@ShiftName,@RecStatus) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ShiftID", model.ShiftID);
                    Db.AddInParameter(cmd, "@ShiftName", model.ShiftName);
                    Db.AddInParameter(cmd, "@RecStatus", model.RecStatus);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool ModifyShift(ShiftEntity model)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [UT_Shift] SET [ShiftName] = @ShiftName WHERE ShiftID=@ShiftID ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ShiftName", model.ShiftName);
                    Db.AddInParameter(cmd, "@ShiftID", model.ShiftID);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public ShiftEntity GetShiftInfo(string id)
        {
            var model = new ShiftEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [ShiftID],[ShiftName],[RecStatus],[InputTime] FROM [UT_Shift] Where ShiftID=@ShiftID ");
            using (DbCommand cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@ShiftID", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["ShiftID"] != DBNull.Value)
                                model.ShiftID = new Guid(reader["ShiftID"].ToString());
                            model.ShiftName = reader["ShiftName"] == obj ? null : Convert.ToString(reader["ShiftName"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }

        public Guid GetShiftId(string shiftName)
        {
            var shiftId = new Guid();
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [ShiftID] FROM [UT_Shift] Where ShiftName=@ShiftName ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@ShiftName", shiftName);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["ShiftID"] != DBNull.Value)
                                shiftId = new Guid(reader["ShiftID"].ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return shiftId;
            }
        }
    }
}
